package com.zebraphp.devtool.SocketUtil;

import com.zebraphp.devtool.DebugBase;
import com.zebraphp.devtool.util.ILog;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Administrator on 2018/4/25.
 * private void sendSocket(String cmd) {
 * //耗时的放入子线程操作
 * try {
 * mSendSocket = new Socket("192.168.1.1", 2001);
 * <p>
 * if (mSendSocket.isConnected()) {
 * ILog.print(Debug.tagGeneral, "socket 连接成功");
 * OutputStream outputStream = mSendSocket.getOutputStream();
 * outputStream.write((cmd).getBytes("utf-8"));
 * outputStream.flush();
 * outputStream.close();
 * } else {
 * ILog.print(Debug.tagGeneral, "socket 连接失败");
 * }
 * <p>
 * } catch (IOException e) {
 * e.printStackTrace();
 * }
 * <p>
 * }
 */

public class TCPSocketUtil {

    private static ExecutorService mExecutorService = null;
    private PrintWriter mPrintWriter;
    private BufferedReader mBufferedReader;

    public static ExecutorService getExecutorsInstance() {
        if (mExecutorService == null) {
            mExecutorService = Executors.newCachedThreadPool();
        }
        return mExecutorService;
    }

    public void connect(String IP, int port) {
        getExecutorsInstance().execute(new ConnectService(IP, port));
    }

    public void connect() {
        getExecutorsInstance().execute(new ConnectService());
    }

    public void send(String msg) {
        getExecutorsInstance().execute(new SendService(msg));
    }

    public void receiveMsg() {
        String recMsg;
        try {
            while (true) {

                if ((recMsg = mBufferedReader.readLine()) != null) {
                    //开启异步线程更新 文本组件
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void disconnect() {
        getExecutorsInstance().execute(new SendService("0"));
    }

    private class SendService implements Runnable {
        private String msg;

        public SendService(String msg) {
            this.msg = msg;
        }

        @Override
        public void run() {
            mPrintWriter.println(msg);
        }
    }

    private class ConnectService implements Runnable {
        private String IP;
        private int port;

        public ConnectService() {
            this.IP = "192.168.1.1";
            this.port = 2001;
        }

        public ConnectService(String IP, int port) {
            this.IP = IP;
            this.port = port;
        }

        @Override
        public void run() {
            try {
                Socket socket = new Socket(IP, port);
                socket.setSoTimeout(60000);

                if (socket.isConnected()) {
                    ILog.print(DebugBase.tagGeneral, "========= Socket 连接成功 =========");
                } else {
                    ILog.print(DebugBase.tagGeneral, "========= Socket 连接失败 =========");
                }
                mPrintWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8")), true);
                mBufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));

                //循环接收消息
                receiveMsg();

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

}
